<!DOCTYPE html>
<script src="../../../resources/js-test.js"></script>
<script>
window.jsTestIsAsync = true;

description('Test that MutationObservers created during delivery must wait for the next loop.');
function finish() {
    shouldBe('order.length', '4');
    shouldBe('order[0]', '1');
    shouldBe('order[1]', '2');
    shouldBe('order[2]', '1');
    shouldBe('order[3]', '3');
    finishJSTest();
}

var order = [];
var div = document.createElement('div');

var observer3;
var observer1 = new MutationObserver(function(mutations) {
    order.push(1);
    if (!observer3) {
        observer3 = new MutationObserver(function(mutations) {
            order.push(3);
        });
        observer3.observe(div, {attributes: true});
        div.setAttribute('foo', 'baz');
    }
});
var observer2 = new MutationObserver(function(mutations) {
    order.push(2);
});

observer1.observe(div, {attributes: true});
observer2.observe(div, {attributes: true});
div.setAttribute('foo', 'bar');
setTimeout(finish, 0);
</script>
